// pages/my/edit-user-info/edit-user-info.js
const app = getApp();
const api = require('../../../utils/util')
import WxValidate from '../../../utils/WxValidate'
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

  /**
   * 页面的初始数据
   */
  data: {
    is_manage: false,
    is_salesman: false,
    form: {
      avatar: '',
      headpic:'',
      name: '',
      usermobile: ''
    },
    fileList: '',
    disabled:''

  },
//   开关
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ is_manage: detail });
  },
  onChange1({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ is_salesman: detail });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  // 初始化表单验证
  initValidate() {
    //验证规则
    const rules = {
      name: {
        required: true
      },
      usermobile: {
        required: true,
        tel: true
      }
    };
    //验证规则提示语，若不传则调用默认的信息
    const messages = {
      name: {
        required: "请输入您的姓名"
      },
      usermobile: {
        required: "请输入您的联系电话",
        tel: "请输入正确的联系电话"
      }
    };

    this.WxValidate = new WxValidate(rules, messages)
  },
  // 通用输入函数
  inputHandle(e) {
    let { name } = e.currentTarget.dataset;
    let value = e.detail;
    let form = this.data.form;
    form[name] = value.trim();
    this.setData({
      form
    });
  },

  // 提交
  submitHandle() {
    let {
      form
    } = this.data;
    // 表单验证
    if (!this.WxValidate.checkForm(this.data.form)) {
      const error = this.WxValidate.errorList[0];
      wx.showToast({
        title: error.msg,
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
        success: (result)=>{
          
        },
        fail: ()=>{},
        complete: ()=>{}
      });
      return false
    }

    this.setData({
      disabled: 'disabled'
    })

    let param = {
      ucode: app.globalData.userinfo.ucode,
      headpic: form.headpic,
      name: form.name,
      usermobile: form.usermobile,
      is_manage:this.data.is_manage,
      is_salesman: this.data.is_salesman
    }
    api.add_enterprise_member(param).then(function(res){
      console.log(res)
      if(res.code == '0'){
        wx.showToast({
          title: '添加成功',
          success:function(){ 
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/enterprise/contact/contact',
              })
             }, 2000)} 
        })
      }else if(res.code == '2'){
        wx.showToast({
          title: '用户已存在！',
          icon: 'error'
        })
      }else{
        wx.showToast({
          title: '添加失败',
          icon: 'error'
        })
      }
    
    })
  },

  afterRead(event) {
    const that = this;
    const { file } = event.detail;
    let {
      form
    } = this.data;
    var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
    wx.uploadFile({
      url: app.globalData.uploadUrl, 
      filePath: file.url,
      name: 'file',
      header:header,
      formData: { user: 'test' },
      success(res) {
        var api_data = JSON.parse(res.data) 
        console.log(api_data.data.pic);
        // 上传完成需要更新 fileList
        form.headpic = api_data.data.pic;
      },
    });
    
    form.avatar = file.url;
    this.setData({
      form
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
